<%-- 
    Document   : signup
    Created on : Dec 4, 2011, 11:35:05 PM
    Author     : Michael
--%>

<%@page import="java.util.Calendar"%>
<%@page import="java.util.Random"%>
<%

    String fname = request.getParameter("firstname");
    String lname = request.getParameter("lastname");
    String email = request.getParameter("femail");
    String pw = request.getParameter("newpassword");
    String gender=request.getParameter("gender");
       
    java.sql.Connection conn = new DBClasses.GetConnection().getConnection();
    try
    {
          java.sql.Statement stmt = conn.createStatement();
          boolean exists = true;
          int custID=-1;
          int accountNo=-1;
          while(exists)
          {
              Random r = new Random();
              r.setSeed(System.currentTimeMillis());
              custID = r.nextInt(1000000);
              accountNo = r.nextInt(1000000);
              java.sql.ResultSet rs = stmt.executeQuery("SELECT CustomerID FROM customer WHERE CustomerID='" + custID + "'" + 
                     " OR AccountNo='" + accountNo + "'");
              if(!rs.next())
                  exists=false;
          }
          
          java.sql.PreparedStatement ps = conn.prepareStatement("INSERT INTO customer "
                  + "(CustomerID, Firstname, Lastname, Sex, Email, CustPassword, CreationDate, AccountNo, Preferences)"
                  + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
          
          ps.setInt(1, custID);
          ps.setString(2, fname);
          ps.setString(3, lname);
          ps.setString(4, gender);
          ps.setString(5, email);
          ps.setString(6, pw);
          ps.setDate(7, new java.sql.Date(System.currentTimeMillis()), Calendar.getInstance());
          ps.setInt(8, accountNo);
          ps.setString(9, "Cars, Music, Electronics, etc...");
          
          System.out.println(ps.toString());
          ps.execute();
    }
    
    catch(Exception e)
    {
        e.printStackTrace();
        response.sendRedirect("index.html");
    }
    finally
    {
        try{
            conn.commit();
            conn.close();
        }
        catch(Exception e) { response.sendRedirect("index.html"); }
    }

%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Account Created</title>
    </head>
    <body>
        <h1>Account has been successfully created!</h1> <br />
        <a href="index.html"> Click Here to Log in </a>     
    </body>
</html>
